package th.co.bjc.md.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONValue;

import th.co.bjc.md.web.bean.Expertise;
import th.co.bjc.md.web.bean.ExpertiseAgent;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class SearchByServlet
 */
public class SearchByServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchByServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("ACTION");
		if(action == null){
			getServletContext().getRequestDispatcher("/jsp/searchby.jsp").forward(request, response);
		}else if(action.equals("EXPERTISEBYREGION")){
			getExpertiseByRegion(request, response);
		}else if(action.equals("EXPERTISEITEM")){
			getExpertiseItem(request, response);
		}
	}
	
	private void getExpertiseByRegion(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String mapLocation = request.getParameter("mapLocation");
		// Query list of expertise by region
		
		//
		ArrayList<Expertise> list = new ArrayList<Expertise>();
		Expertise e1 = new Expertise();
		e1.setExpertiseId(1);
		e1.setExpertiseName("TEST1");
		e1.setRegion("NORTH");
		list.add(e1);
		Expertise e2 = new Expertise();
		e2.setExpertiseId(2);
		e2.setExpertiseName("TEST2");
		e2.setRegion("NORTH");
		list.add(e2);
		JSONArray jsonObject = JSONArray.fromObject(list);
		out.print(jsonObject);
	}
	
	private void getExpertiseItem(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int expertiseId = Integer.parseInt(request.getParameter("expertiseId"));
		// Query list of expertiseAgent by expertiseId
		
		//
		Expertise e = new Expertise();
		e.setExpertiseId(1);
		e.setExpertiseName("TEST1");
		e.setRegion("NORTH");
		
		ExpertiseAgent ea1 = new ExpertiseAgent();
		ea1.setExpertiseAgentId(1);
		ea1.setLocation("Chiangmai");
		ea1.setName("Micheal");
		ea1.setPosition("MD");
		ea1.setTelephone("0917196810");
		ea1.setEmail("korapotu@gmail.com");
		e.setExpertiseAgentList(ea1);
		
		ExpertiseAgent ea2 = new ExpertiseAgent();
		ea2.setExpertiseAgentId(2);
		ea2.setLocation("Chiangrai");
		ea2.setName("David");
		ea2.setPosition("Sales");
		ea2.setTelephone("0806806256");
		ea2.setEmail("chodkeengon@gmail.com");
		e.setExpertiseAgentList(ea2);
		
		ExpertiseAgent ea3 = new ExpertiseAgent();
		ea3.setExpertiseAgentId(3);
		ea3.setLocation("Chiangtung");
		ea3.setName("Joseph");
		ea3.setPosition("Admin");
		ea3.setTelephone("0895614324");
		ea3.setEmail("joseph.e@gmail.com");
		e.setExpertiseAgentList(ea3);
		
		ExpertiseAgent ea4 = new ExpertiseAgent();
		ea4.setExpertiseAgentId(4);
		ea4.setLocation("Chiangrung");
		ea4.setName("Mata");
		ea4.setPosition("Player");
		ea4.setTelephone("0861883527");
		ea4.setEmail("mata.j@gmail.com");
		e.setExpertiseAgentList(ea4);
		JSONArray jsonObject = JSONArray.fromObject(e);
		out.print(jsonObject);
	}
}
